package org.chen.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.chen.domain.entity.VideoArticle;
import org.chen.domain.vo.ArticleVO;

import java.util.List;

@Mapper
public interface ArticleMapper extends BaseMapper<VideoArticle> {
    @Update("UPDATE video_article SET status = #{status} WHERE id = #{articleId}")
    int updateStatus(@Param("articleId") String articleId, @Param("status") Integer status);

    /**
     * 查询文章详情（包含视频信息）
     */
    ArticleVO selectArticleWithVideo(@Param("id") String id);

    /**
     * 查找已存在的标题
     */
    @Select({
            "<script>",
            "SELECT title FROM video_article WHERE title IN",
            "<foreach collection='titles' item='title' open='(' separator=',' close=')'>",
            "#{title}",
            "</foreach>",
            "</script>"
    })
    List<String> findExistingTitles(@Param("titles") List<String> titles);

    /**
     * 批量插入文章
     */
    @Insert({
            "<script>",
            "INSERT INTO video_article (id, title, description, source, article_timestamp, status, create_time, category_id, category_path) VALUES ",
            "<foreach collection='articles' item='article' separator=','>",
            "(#{article.id}, #{article.title}, #{article.description}, #{article.source}, #{article.articleTimestamp}, ",
            "#{article.status}, NOW(), #{article.categoryId}, #{article.categoryPath})",
            "</foreach>",
            "</script>"
    })
    int batchInsert(@Param("articles") List<VideoArticle> articles);

    @Update({
            "<script>",
            "UPDATE video_article",
            "SET status = #{status}",
            "WHERE id IN",
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</script>"
    })
    void batchUpdateStatus(@Param("ids") List<String> ids, @Param("status") Integer status);
}
